import { createRouter, createWebHistory } from "vue-router";
import login from '../view/login.vue';
import main from '../view/main.vue'
import {ElMessage, ElNotification} from "element-plus";
import {h} from "vue";
import user from '../view/user.vue'
import userinfo from '../view/userinfo.vue'
import updatepwd from '../view/updatepwd.vue'
import updateimg from '../view/uoloadimg.vue'
import updateuser from '../view/updateUser.vue'
import article from '../view/article.vue'
import LogCtroller from '../view/logController.vue'
import  Article from '../view/Article/ArticleList.vue'
import shop from  '../view/Shop/shop.vue'
import index from '../view/Index.vue'
import app1 from '../view/Index2.vue'
import heartcure from '../view/Heart.vue'
import  chat3 from '../view/chat3.vue'
import  ppt from  '../view/ppt.vue'
const routes = [
    {
        path: '/',
       redirect:'/index'
    },
    {
        path: '/login',
        name: 'login',
        component: login
    },
    {
        path: "/app",
        component: app1,
        meta: {        //是路由的原数据
            requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
        children:[
            {
                path: '/article',
                name:'article',
                component: article
            },
            {
            path: '/heart',
                component:heartcure
            },
            {
                path:'/chat3',
                component: chat3
            },
            {
              path: '/ppt',
              component: ppt
            },

            {
                path: '/main',
                name: 'main',
                component: main
            },
            {
                path: '/shop',
                name: 'shop',
                component: shop
            },
            {
                path: '/user',
                name:'user',
                component: user,

                children:[
                    {
                        path:'userinfo',
                        component:userinfo
                    },
                    {
                        path: 'updatepwd',
                        component:updatepwd
                    },
                    {
                        path: 'updateuser',
                        component: updateuser
                    },
                    {
                        path: 'updateimg',
                        component: updateimg
                    },{
                        path: 'LogController',
                        component:LogCtroller
                    }
                ]
            },
            {
                path: '/list',
                name: 'list',
                component: Article
            }
        ]
    },

    {
        path: "/index",
        component: index

    }

];

const router = createRouter({
    history: createWebHistory(),
   routes
});

router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {
        if (!localStorage.getItem("token")) {
            //判断是否有token
            if (to.path === "/login") {
                ElMessage({message: "客官您还未登录呢", type: 'warning'})
                //如果是去往登录直接放行
                next()
            } else {
                next("/login")
                ElMessage({message: "客官您还未登录呢", type: 'warning'})

                //如果是其他路由节点切换到登录注册
            }

        } else {
            console.log(localStorage.getItem("token"))
            console.log("有token继续放行")
            next()
        }
    }else {
        next();
    }
}
);

/*
* 在路由配置文件中，我们使用createRouter函数创建一个路由实例，并通过createWebHistory函数来创建一个浏览器历史记录模式。然后，在routes数组中配置具体的路由信息，包括路径、名称和对应的组件
* */
export default router;
